package com.xhwl.centiotdata.handler.device.impl;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.handler.device.DeviceIntegrationFactory;
import com.xhwl.centiotdata.handler.device.DeviceIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.device.DeviceIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.dnake.DeviceDnakeDTO;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.suppliersystem.dnake.DeviceDnakeHandler;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.dto.cent.pedes.DeviceAttributeDTO;
import com.xhwl.common.enums.device.DeviceTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: wuyuhao
 * @createDate: 2023/5/4 13:36
 */
@Slf4j
@Service
public class DeviceIntegrationDnakeHandler extends DeviceIntegrationHandler {

    @Resource
    IDeviceCentService deviceCentService;

    @Resource
    DeviceDnakeHandler deviceDnakeHandler;

    @Override
    public void afterPropertiesSet() throws Exception {
        // 狄耐克人行
        DeviceIntegrationFactory.register(101, this);
    }

    @Override
    public void pullDevice(DeviceIntegrationConfigDTO configDTO) {
        if (configDTO.getDeviceType() == null) {
            log.error("projectId：{} pullDevice failed! deviceType is null!", configDTO.getProjectId());
            return;
        }
        if (!configDTO.getDeviceType().equals(DeviceTypeEnum.DOOR.getCode())) {
            return;
        }
        // 拉取数据
        List<DeviceDnakeDTO> deviceDnakeDTOList = deviceDnakeHandler.pullDevice(configDTO.getProjectId());
        if (CollectionUtils.isEmpty(deviceDnakeDTOList)) {
            return;
        }
        // 转换数据
        List<DeviceDTO> deviceDTOList = this.handleDevice(configDTO, deviceDnakeDTOList);
        // 存储业务中台
        deviceCentService.asyncUpdateDevice(deviceDTOList);
    }

    private List<DeviceDTO> handleDevice(DeviceIntegrationConfigDTO configDTO, List<DeviceDnakeDTO> deviceDnakeDTOList) {
        return deviceDnakeDTOList.stream()
                .map(deviceDnakeDTO -> {
                    DeviceDTO deviceDTO = new DeviceDTO();
                    deviceDTO.setProjectId(configDTO.getProjectId());
                    deviceDTO.setProductId(configDTO.getProductId());
                    deviceDTO.setCategoryId(configDTO.getCategoryEnum().getId());
                    deviceDTO.setDeviceTypeId(configDTO.getDeviceType());
                    String name = deviceDnakeDTO.getBuild() + "栋" + deviceDnakeDTO.getUnit() + "单元" + deviceDnakeDTO.getFloor() + "楼" + deviceDnakeDTO.getFamily() + "房";
                    deviceDTO.setName(name);
                    deviceDTO.setThirdDeviceId(deviceDnakeDTO.getUid());
                    deviceDTO.setOnlineStatus(deviceDnakeDTO.getOnline());
                    // 路址处理，如果有第二个，那么默认是路址id
                    String[] split = deviceDnakeDTO.getItemNumber().split("-");
                    if (split.length > 1 && split[1].matches("\\d+")) {
                        String locationId = split[1];
                        deviceDTO.setOrganizationId(Integer.parseInt(locationId));
                    }
                    JSONObject attributeJson = new JSONObject();
                    // 人行设备
                    if (configDTO.getDeviceType().equals(DeviceTypeEnum.DOOR.getCode())) {
                        DeviceAttributeDTO deviceAttributeDTO = new DeviceAttributeDTO();
                        deviceAttributeDTO.setIp(deviceDnakeDTO.getIp());
                        attributeJson = (JSONObject) JSONObject.toJSON(deviceAttributeDTO);
                    }
                    deviceDTO.setAttributeJson(attributeJson);
                    return deviceDTO;
                }).collect(Collectors.toList());
    }

}
